package com.ny.zmb.openapi.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ny.zmb.glxt.entity.PmStore;
import com.ny.zmb.glxt.mapper.PmStoreMapper;
import com.ny.zmb.openapi.controller.entity.Result;
import com.ny.zmb.openapi.entity.PmApiLog;
import com.ny.zmb.openapi.entity.PmContract;
import com.ny.zmb.openapi.entity.PmRegister;
import com.ny.zmb.openapi.entity.PmRegisterExamine;
import com.ny.zmb.openapi.mapper.PmContractMapper;
import com.ny.zmb.openapi.mapper.PmRegisterMapper;
import com.ny.zmb.openapi.service.IPmApiLogService;
import com.ny.zmb.openapi.service.PmContractSignService;
import com.ny.zmb.openapi.service.PmRegisterExamineService;
import com.ny.zmb.openapi.service.PmRegisterService;
import com.ny.zmb.openapi.ysutil.YsHttpUtil;
import com.ny.zmb.openapi.ysutil.common.ServiceEnum;
import com.ny.zmb.openapi.ysutil.common.SubCodeEnum;
import com.ny.zmb.openapi.ysutil.common.YsPayConstant;
import com.ny.zmb.openapi.ysutil.from.BizReqJsonCollege.*;
import com.ny.zmb.openapi.ysutil.utils.ValidateUtils;
import com.ny.zmb.openapi.ysutil.utils.YsChannelClientException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 合同
 *
 * @author ChenZhiZheng
 * @date 20241108
 */
@Service
public class PmContractSignServiceImpl implements PmContractSignService {
    @Resource
    private YsHttpUtil ysHttpUtil;
    @Resource
    private PmContractMapper pmContractMapper;

    QueryWrapper<PmContract> pmContractQueryWrapper = new QueryWrapper<>();

    @Override
    public Result contractSign(JSONObject obj, String srcMerchantNo) {
        String requestNo = obj.getString("requestNo");
        String merchantNo = obj.getString("merchantNo");
        String isSendMsg = obj.getString("isSendMsg");
        String returnUrl = obj.getString("returnUrl");
        String notifyUrl = obj.getString("notifyUrl");
        JSONObject result = doActive(ServiceEnum.CONTRACT_SIGN, null, obj);
        PmContract pmContract = new PmContract(requestNo, merchantNo, isSendMsg, returnUrl, notifyUrl);
        pmContractMapper.insert(pmContract);
        pmContractMapper.updateById(pmContract.update(result));
        return Result.ok(result);
    }


    @Override
    public Result contractQuery(JSONObject obj, String srcMerchantNo) {
        String requestNo = obj.getString("requestNo");
        String merchantNo = obj.getString("merchantNo");
        // 发送请求
        JSONObject result = doActive(ServiceEnum.CONTRACT_QUERY, null, obj);
        // 新建对象
        PmContract pmContract = new PmContract(requestNo, merchantNo);
        pmContractMapper.insert(pmContract.update(result));
        return Result.ok(result);
    }

    @Override
    public Result cancelContractSign(JSONObject obj, String srcMerchantNo) {
        String requestNo = obj.getString("requestNo");
        String merchantNo = obj.getString("merchantNo");
        JSONObject result = doActive(ServiceEnum.CANCEL_CONTRACT_SIGN, null, obj);
        String subMsg = obj.getString("subMsg") == null || obj.getString("subMsg").isEmpty() ? null : obj.getString("subMsg");
        subMsg = "取消合同" + subMsg;
        obj.put("subMsg", subMsg);
        PmContract pmContract = new PmContract(requestNo, merchantNo);
        pmContractMapper.insert(pmContract.update(result));
        return Result.ok(result);
    }

    @Override
    public Result downloadContract(JSONObject obj, String srcMerchantNo) {
        String requestNo = obj.getString("requestNo");
        String merchantNo = obj.getString("merchantNo");
        JSONObject result = doActive(ServiceEnum.DOWNLOAD_CONTRACT, null, obj);
        return Result.ok(result);
    }

    private JSONObject doActive(ServiceEnum anyEnum, String srcMerchantNo, Object obj) {
        try {
            return ysHttpUtil.sendBase(anyEnum, "prd", null, null, null, srcMerchantNo, obj);
        } catch (Exception var) {
            throw new RuntimeException(var.getMessage());
        }
    }

}


